<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card>
      <!-- 顶部搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @keyup.native.enter="getOrderList"
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 数据表格区域 -->
      <el-row>
        <el-table :data="orderList" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column
            label="订单编号"
            prop="order_number"
          ></el-table-column>
          <el-table-column
            label="订单价格"
            prop="order_price"
            width="80"
          ></el-table-column>
          <el-table-column label="是否付款" width="80">
            <template slot-scope="scope">
              <el-tag type="danger" v-if="scope.row.pay_status === '0'"
                >未付款</el-tag
              >
              <el-tag type="primany" v-else>已付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="是否发货"
            prop="is_send"
            width="80"
          ></el-table-column>
          <el-table-column label="下单时间" width="150">
            <template slot-scope="scope">
              {{ scope.row.create_time | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showEditDialog(scope)"
              ></el-button>
              <el-button
                type="success"
                icon="el-icon-location-outline"
                size="mini"
                @click="showProgressDialog"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <!-- 分页区域 -->
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-row>
    </el-card>

    <!-- 修改地址的对话框区域 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <!-- 修改地址的表单 -->
      <el-form
        :model="editFrom"
        :rules="editFromRules"
        ref="editFromRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县:" prop="address1">
          <el-cascader
            clearable
            v-model="editFrom.address1"
            :options="catyData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址:" prop="address2">
          <el-input v-model="editFrom.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 展示物流信息对话框区域 -->
    <el-dialog
      title="展示物流信息"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <el-timeline>
        <el-timeline-item
          placement="top"
          v-for="(item, index) in progressInfo"
          :key="index"
          :timestamp="item.time"
        >
          <el-card>
            {{ item.context }}
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import catyData from './citydata.js'
export default {
  data() {
    return {
      // 导入省市区/县的数据
      catyData,
      // 获取订单列表的参数对象
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示几条数据
        pagesize: 5
      },
      // 订单列表数据对象
      orderList: [],
      total: 0,
      // 控制修改地址对话框显示隐藏的flag
      editDialogVisible: false,
      // 修改地址的表单对象
      editFrom: {
        address1: [],
        address2: ''
      },
      // 修改地址的验证规则对象
      editFromRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 控制展示物流信息的对话框显示隐藏的flag
      progressDialogVisible: false,
      // 缓存物流信息对象的数据
      progressInfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表数据
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败!')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听 pageSize 事件
    handleSizeChange(newPage) {
      // 修改当前页面要显示几条数据
      this.queryInfo.pagesize = newPage
      // 从新发送请求获取数据
      this.getOrderList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      // 当页码改变的时候
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 监听 修改地址对话框的显示事件
    showEditDialog() {
      this.editDialogVisible = true
    },
    // 监听 修改地址对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFromRef.resetFields()
    },
    // 监听 展示物流对话框的显示事件
    async showProgressDialog() {
      const { data: res } = await this.$http.get('/kuaidi/' + '1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('查询物流信息失败!')
      }
      this.progressInfo = res.data
      console.log(this.progressInfo)
      this.progressDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
